﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI"/>
/// <reference path="../../Manager/Admin/Admin.asmx" />
Type.registerNamespace("motobankcorp");

motobankcorp.Bridge_CollectionInfo = function(element) {
  motobankcorp.Bridge_CollectionInfo.initializeBase(this, [element]);
  this.__hoverDeferments = "hvrDeferments";
  this._initProps({ _loanID: 0, _usixID: '', _companyID: 0 });
  this._initEvents(["loaded"]);
}

motobankcorp.Bridge_CollectionInfo.prototype = {
  // Methods
  updateBorrower: function(eventArgs) {
    var t = eventArgs.target;
    var fldName = t.getAttribute("dataFld");
    var borrowerID = $J(t).parents("[borrowerID]").attr("borrowerID");
    if (!confirm("Update " + fldName + " to:[" + t.value + "] for borrower:" + borrowerID)) return false;
    Dimok.FindWSMethod("Borrower_Update", WSA)(t.dataFld, eventArgs.target.value, borrowerID, this.get_loanID(), Function.createDelegate(this, _cbOk), Function.createDelegate(this, _cbFailed), eventArgs.target);
    this._showLoadPanel(this, "Updating ...");
    function _cbOk(result, context) {
      this._hideLoadPanel();
      alert(fldName + " updated.");
    }
    function _cbFailed(error, context) {
      this._hideLoadPanel();
      alert(fldName + " update failed!!!.\n\n" + error.get_message());
      var val = context.value;
      context.value = context.defaultValue;
      $J(context).focus().val(val).select();
    }
  },
  fetchHTML: function(loanID, usixID, companyID) {
    //if (!loanID && !usixID)return $J(this.get_element()).slideUp();
    //$J(this.get_element()).stop().slideDown();
    this.set_loanID(loanID); this.set_usixID(usixID); this.set_companyID(companyID);
    Dimok.FindWSMethod("CollectionInfo", WSA)(loanID, usixID || "", companyID || 0, Function.createDelegate(this, this.dataBind), this.get_wsErrorDelegate());
    this._showLoadPanel();
  },
  dataBind: function(result) {
    this._unWireEvents();
    var toolTip = $find(this.__hoverDeferments);
    if (toolTip) toolTip.dispose();
    this.get_element().innerHTML = result;
    var defLbl = $J(".DefermentDate")[0];
    var defPnl = $J(".DefermentDates")[0];
    if (defLbl)
      $create(AjaxControlToolkit.HoverMenuBehavior,
       { "PopupPosition": 3, "id": this.__hoverDeferments, "popupElement": defPnl }, null, null, defLbl);
    this._wireEvents();
    this._hideLoadPanel();
  },
  _getUpdatables: function() { return $J("input[dataFld]", this.get_element()); },
  _wireEvents: function() {
    for (var i = 0, els = this._getUpdatables(); i < els.length; i++)
      this.addHandlers(els[i], { 'change': this.updateBorrower }, this);
  },
  _unWireEvents: function() {
    this.clearHandlers();
    //    for (var i = 0, els = this._getUpdatables(); i < els.length; i++)
    //      $removeHandler(els[i], 'change', this.updateBorrower);
  },
  _onLoaded: function() {
    var pe = this.findPageEventsGlobal();
    if (!pe) return;
    pe.add_usixDeselected(Function.createDelegate(this, _bridgeDeSelected));
    pe.add_usixSelected(Function.createDelegate(this, _bridgeSelected));
    var This = this;
    function _bridgeSelected(sender, eventArgs) {
      var keys = eventArgs.get_keys();
      this.fetchHTML(keys.get_LoanID(), keys.get_UsixID(), keys.get_CompanyID_Raw());
      $J(this.get_element()).show(); //.stop().slideDown();
    }
    function _bridgeDeSelected() {
      $J(this.get_element()).hide(); //.stop().slideUp("slow"); //, function() { This.fetchHTML(0, '', 0); });
      //$J(this.get_element()).parent().stop().slideUp("fast"); //, function() { This.fetchHTML(0, '', 0); });
    }
  },
  initialize: function() {
    motobankcorp.Bridge_CollectionInfo.callBaseMethod(this, 'initialize');
    this._wireEvents();
    this._onLoaded();
    this.raise_loaded();
  },
  dispose: function() {
    this._unWireEvents();
    motobankcorp.Bridge_CollectionInfo.callBaseMethod(this, 'dispose');
    //var toolTip = $find(this.__hoverDeferments); if (toolTip) toolTip.dispose();
  }
}

motobankcorp.Bridge_CollectionInfo.registerClass('motobankcorp.Bridge_CollectionInfo', motobankcorp.UI.Control);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
